package web.Validators;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import auth.service.User;

import web.LoginFormController;

public class LoginFormValidator implements Validator {
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	@Override
	public boolean supports(Class clazz) {
		return User.class.equals(clazz);
	}
	
	
	/**
	@Override
	public void validate(Object obj, Errors errors) {
		User  user = (User) obj;
		if ((user.getLogin() == null)||(user.getMotDePasse()==null)) {
			if(user.getLogin() == null)
				errors.rejectValue("login", "error.not-specified", null, "Value required.");
			if(user.getMotDePasse() == null)
				errors.rejectValue("motDePasse", "error.not-specified", null, "Value required.");

		}
		else {
			logger.info("Validating with " +user.getEmail());
			String login=user.getLogin();
			boolean b=true;
			for(int i=2; i<user.getLogin().length();i++)
			{
				if(login.charAt(i)=='@')
					b=false;		

			}
			if(b==true)
				errors.rejectValue("login", "error.not-mail", null, "Value required.");

		}*/

	
	public void validate(Object obj, Errors errors){
		User user = (User)obj;
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "field.required", "le login est oblgatoire");
		 
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "motDePasse", "field.required", "Le mot de passe est obligatoire");
		if ( ! errors.hasFieldErrors("login")) {
			
			String login=user.getLogin();
			
			boolean b=true;
			for(int i=2; i<user.getLogin().length();i++)
			{
			
				if(login.charAt(i)=='@')
					b=false;		

			}
			if(b==true)
				errors.rejectValue("login", "not_zero", "ce n'est pas un mail");
		}
	
}
	}
	
	

